package com.cytech.cloudclinic.moudle.main.setting.prescription;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.PrescriptionDrugAdapter;
import com.cytech.cloudclinic.adapter.TemplateAdapter;
import com.cytech.cloudclinic.adapter.provide.FirstNode;
import com.cytech.cloudclinic.adapter.provide.SecondNode;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.bean.CustomPrescriptionEntity;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.bean.PrescriptionTag;
import com.cytech.cloudclinic.bean.PrescriptionTemplate;
import com.cytech.cloudclinic.bean.event.PrescriptionTagClick;
import com.cytech.cloudclinic.bean.event.RefreshPresctiptionTemplate;
import com.cytech.cloudclinic.dialog.AddPrescriptionTemplateDialog;
import com.cytech.cloudclinic.moudle.main.setting.prescription.drugs.PrescriptionDrugActivity;
import com.cytech.cloudclinic.moudle.main.setting.prescription.group.PrescriptionGroupActivity;
import com.yechaoa.yutils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_1;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_2;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_3;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_4;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_5;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_6;

/**
 * 我的处方模板
 */
public class PrescriptionModuleActivity extends BaseActivity<PrescriptionModulePresenter> implements PrescriptionModuleView {

    @BindView(R.id.rv_prescription_template_menu)
    RecyclerView menuRv;
    @BindView(R.id.rv_prescription_template_drug)
    RecyclerView drugRv;
    @BindView(R.id.tv_prescription_tag_name)
    TextView tagNameTv;
    @BindView(R.id.rg_admission_manager)
    RadioGroup rgAdmissionManager;

    private List<Drug> allDrugList;
    private PrescriptionDrugAdapter drugAdapter;
    private TemplateAdapter templateAdapter;

    private int drugTagId = DRUG_TAG_1;
    //处方模板分类id
    private String currentPrescriptionType;
    //当前处方模板id
    private String currentPrescriptionId;
    //当前处方模板名称
    private String currentPrescriptionName;
    //处方对应的药品
    private List<CustomPrescriptionEntity> customPrescriptionEntityList;


    public static void actionStart(Context context){
        Intent intent = new Intent(context,PrescriptionModuleActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected PrescriptionModulePresenter createPresenter() {
        return new PrescriptionModulePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_prescription_module;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);

        rgAdmissionManager.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_admission_1:
                        drugTagId = DRUG_TAG_1;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                    case R.id.rb_admission_2:
                        drugTagId = DRUG_TAG_2;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                    case R.id.rb_admission_3:
                        drugTagId = DRUG_TAG_3;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                    case R.id.rb_admission_4:
                        drugTagId = DRUG_TAG_4;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                    case R.id.rb_admission_5:
                        drugTagId = DRUG_TAG_5;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                    case R.id.rb_admission_6:
                        drugTagId = DRUG_TAG_6;
                        presenter.getPrescriptionTags(drugTagId);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        initDrugList();
        presenter.getPrescriptionTags(drugTagId);
    }

    private void initDrugList(){
        allDrugList = new ArrayList<>();
        drugAdapter = new PrescriptionDrugAdapter(allDrugList);
        drugRv.setLayoutManager(new LinearLayoutManager(this));
        drugRv.setAdapter(drugAdapter);
    }

    @OnClick(R.id.titlebar_back)
    public void exit(View view){
        finish();
    }

    @OnClick(R.id.prescription_module_add)
    public void addTemplate(View view){
        //添加模板
        AddPrescriptionTemplateDialog addPrescriptionTemplateDialog = new AddPrescriptionTemplateDialog(drugTagId);
        addPrescriptionTemplateDialog.show(getSupportFragmentManager(),"ADD_PRESENTER");
    }

    @OnClick(R.id.prescription_module_group)
    public void editGroup(View view){
        //编辑分组
        PrescriptionGroupActivity.actionStart(this,drugTagId);
    }

    @OnClick(R.id.tv_prescription_edit_drug)
    public void editDrug(View view){
        PrescriptionDrugActivity.actionStart(this,drugTagId,currentPrescriptionType,currentPrescriptionName,currentPrescriptionId,customPrescriptionEntityList);
    }

    @Override
    public void getPrescriptionSuccess(List<PrescriptionTag> result) {
        //处方模板分类
        List<BaseNode> menuList = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            //SecondNode 的 list
            List<BaseNode> secondNodeList = new ArrayList<>();
            for (int n = 0; n < result.get(i).diagnoses.size(); n++) {
                SecondNode seNode = new SecondNode(result.get(i).diagnoses.get(n).diagnose,result.get(i).diagnoses.get(n).prescription_id,result.get(i).diagnoses.get(n).prescription_type);
                secondNodeList.add(seNode);
            }

            FirstNode entity = new FirstNode(secondNodeList, result.get(i).value);
            entity.setExpanded(i == 0);
            menuList.add(entity);
        }
        templateAdapter = new TemplateAdapter();
        templateAdapter.setNewInstance(menuList);
        menuRv.setLayoutManager(new LinearLayoutManager(this));
        menuRv.setAdapter(templateAdapter);

        if(result!=null&&result.size()>0){
            List<PrescriptionTemplate> list = result.get(0).diagnoses;
            if(list!=null&&list.size()>0){
                tagNameTv.setText(list.get(0).diagnose);
                currentPrescriptionType = list.get(0).prescription_type;
                currentPrescriptionName =list.get(0).diagnose;
                presenter.getPrescriptionItem(list.get(0).prescription_id);
            }else{
                //如果分类没有数据，药品对应的也不应该显示
                tagNameTv.setText("");
                drugAdapter.setNewInstance(null);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshData(RefreshPresctiptionTemplate event) {
        presenter.getPrescriptionTags(drugTagId);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(PrescriptionTagClick event) {
        //item点击监听
        //处方分类id
        currentPrescriptionName = event.text;
        currentPrescriptionType = event.type;
        presenter.getPrescriptionItem(event.id);
        tagNameTv.setText(event.text);
    }


    @Override
    public void getPrescriptionDrugSuccess(List<Drug> drugs,String prescriptionId) {
        currentPrescriptionId = prescriptionId;
        customPrescriptionEntityList = new ArrayList<>();
        //获取处方详情
        Log.d("TAG", "getPrescriptionDrugSuccess: ");
        drugAdapter.setNewInstance(null);
        for(int i=0;i<drugs.size();i++){
            drugAdapter.addData(drugs.get(i).list);
            CustomPrescriptionEntity customPrescriptionEntity = new CustomPrescriptionEntity();
            customPrescriptionEntity.drugList = drugs.get(i).list;
            customPrescriptionEntityList.add(customPrescriptionEntity);
        }
        //drugAdapter.setNewInstance(allDrugList);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
